package com.lightjet.macross.base.gateway.controller;

import com.lightjet.macross.base.gateway.dto.GatewayFilterDefinition;
import com.lightjet.macross.base.gateway.dto.GatewayPredicateDefinition;
import com.lightjet.macross.base.gateway.dto.GatewayRouteDefinition;
import com.lightjet.macross.base.gateway.service.impl.DynamicRouteServiceImpl;
import com.lightjet.macross.common.dto.ResponseDTO;
import com.lightjet.macross.common.util.RespUtil;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import javax.xml.ws.Response;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : lijia
 * @version : 1.0 2020-05-09 12:22
 * description : 动态路由控制器类
 */

@RestController
@RequestMapping("/route")
@Log
public class GateWayRouteController {

    @Resource(name = "dynamicRouteService")
    private DynamicRouteServiceImpl dynamicRouteService;

    //增加路由
    @PostMapping("")
    public ResponseDTO<String> add(@RequestBody GatewayRouteDefinition gwdefinition) {
        try {
            RouteDefinition definition = assembleRouteDefinition(gwdefinition);
            return this.dynamicRouteService.add(definition);
        } catch (Exception e) {
            e.printStackTrace();
            return RespUtil.CreateError("转换路由失败："+ e.getMessage());
        }

    }
    //删除路由
    @DeleteMapping("/{id}")
    public ResponseDTO<String> delete(@PathVariable String id) {
        try {
            log.info("正在删除路由【"+id+"】");
            return this.dynamicRouteService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return RespUtil.CreateError("删除路由失败："+ e.getMessage());
        }
    }
    //更新路由
    @PutMapping("")
    public ResponseDTO<String> update(@RequestBody GatewayRouteDefinition gwdefinition) {
        try
        {
            RouteDefinition definition = assembleRouteDefinition(gwdefinition);
            return this.dynamicRouteService.update(definition);

        }
        catch (Exception e)
        {
            e.printStackTrace();
            return RespUtil.CreateError("转换路由失败"+ e.getMessage());
        }

    }

    //把传递进来的参数转换成路由对象
    private RouteDefinition assembleRouteDefinition(GatewayRouteDefinition gwdefinition) {
        RouteDefinition definition = new RouteDefinition();
        definition.setId(gwdefinition.getId());
        definition.setOrder(gwdefinition.getOrder());

        //设置断言
        List<PredicateDefinition> pdList=new ArrayList<>();
        List<GatewayPredicateDefinition> gatewayPredicateDefinitionList=gwdefinition.getPredicates();
        for (GatewayPredicateDefinition gpDefinition: gatewayPredicateDefinitionList) {
            PredicateDefinition predicate = new PredicateDefinition();
            predicate.setArgs(gpDefinition.getArgs());
            predicate.setName(gpDefinition.getName());
            pdList.add(predicate);
        }
        definition.setPredicates(pdList);

        //设置过滤器
        List<FilterDefinition> filters = new ArrayList();
        List<GatewayFilterDefinition> gatewayFilters = gwdefinition.getFilters();
        for(GatewayFilterDefinition filterDefinition : gatewayFilters){
            FilterDefinition filter = new FilterDefinition();
            filter.setName(filterDefinition.getName());
            filter.setArgs(filterDefinition.getArgs());
            filters.add(filter);
        }
        definition.setFilters(filters);

        URI uri = null;
        if(gwdefinition.getUri().startsWith("http")){
            uri = UriComponentsBuilder.fromHttpUrl(gwdefinition.getUri()).build().toUri();
        }else{
            // uri为 lb://consumer-service 时使用下面的方法
            uri = URI.create(gwdefinition.getUri());
        }
        definition.setUri(uri);
        return definition;
    }


}
